package eu.epfc.dnet.mastermind.controller;

import eu.epfc.dnet.mastermind.model.beans.RankingBean;
import eu.epfc.dnet.mastermind.services.RankingManager;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Pierre Buvé & Nicolas Van Damme
 */
@Controller
public class RankingsController {

    private RankingManager rankingManager;

    /**
     * Injection du service rankingManager
     * @param rankingManager 
     */
    @Autowired
    public void setRankingManager(RankingManager rankingManager) {
        this.rankingManager = rankingManager;
    }

    /**
     * Methode appellée lors de l'appel de la page rankings.htm via une requete de type GET
     * @param hsr
     * @param rankingModel
     * @return
     * @throws Exception 
     */
    @RequestMapping("/rankings")
    protected ModelAndView displayRankings(HttpServletRequest hsr, Model rankingModel) throws Exception {

        if (hsr.getSession().getAttribute("username") != null) {
            // Si le paramètre username est stocké dans la session, demande l'affichage du bouton de déconnexion.
            String decoButton = "Déconnecter "+ hsr.getSession().getAttribute("username");
            rankingModel.addAttribute("decobutton",decoButton);
        }
        
        List<RankingBean> rankingBestTimeList;
        List<RankingBean> rankingWorstTimeList;
        List<RankingBean> rankingTotalTimeList;
        List<RankingBean> rankingAvgTimeVictoryList;

        // on récupère les valeurs auprès des services concernés.
        
        rankingBestTimeList = rankingManager.getRankingBestTimeList();
        rankingWorstTimeList = rankingManager.getRankingWorstTimeList();
        rankingTotalTimeList = rankingManager.getRankingTotalTimeList();
        rankingAvgTimeVictoryList = rankingManager.getRankingAvgVictoryList();
       
        // on ajoute les valeurs au model.
       
        rankingModel.addAttribute("rankingTotalTimeList", rankingTotalTimeList);
        rankingModel.addAttribute("rankingBestTimeList", rankingBestTimeList);
        rankingModel.addAttribute("rankingWorstTimeList", rankingWorstTimeList);
        rankingModel.addAttribute("rankingAvgTimeVictoryList", rankingAvgTimeVictoryList);

        //on envoie le model.
        
        return new ModelAndView("rankings", "model", rankingModel);
    }
}
